#pragma once
#include <iostream>
using namespace std;
#include<assert.h>
#include"reverse_iterator.h"
namespace hlh {
	template<class T>
	class vector
	{
	public:

		typedef T* iterator;
		typedef const T* const_iterator;
		typedef Reverse_iterator<iterator, T&, T*>reverse_iterator;
		typedef Reverse_iterator<const_iterator, const T&, const T*>const_reverse_iterator;

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}

		vector() 
			:_start(nullptr)
			,_finish(nullptr)
			,_endofstorage(nullptr)
		{ }

		vector(const vector<T>& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			reserve(v.capacity());
			for (const auto &ch:v)
			{
				push_back(ch);
			}
		}

		/*vector(const vector<T>& v)
		{
			_start = new T[v.capacity()];
			memcpy(_start, v._start, sizeof(T) * v.size());
			_finish = _start + v.size();
			_endofstorage = _start + v.capacity();
		}*/

		template<class InputIterator>
		vector(InputIterator first,InputIterator last)
		{
			while (*first!=*last)
			{
				push_back(*first);
				first++;
			}
		}

		vector(size_t n, T val = T()) {
			resize(n, val);
		}

		//vector<int> v2(v1.begin(),v1.end())

		void swap(vector<T>& v) 
		{
			std::swap(_start,v._start);
			std::swap(_finish,v._finish);
			std::swap(_endofstorage,v._endofstorage);
		}

		vector<int>& operator= (vector<T> v)
		{
			swap(v);
			return *this;
		}

		~vector()
		{
			if (_start)
			{
				delete[]_start;
				_start = _finish = _endofstorage = nullptr;
			}
		}
		
		iterator begin() {
			return _start;
		}

		iterator end() {
			return _finish;
		}

		const_iterator begin() const{
			return _start;
		}

		const_iterator end() const{
			return _finish;
		}

		void reserve(size_t n) 
		{
			if (n>capacity())
			{
				int old = size();
				T* tmp = new T[n];
				if (_start != nullptr)
				{
					//memcpy(tmp, _start, old * sizeof(T));
					for (size_t i = 0; i < old; i++)
					{
						tmp[i] = _start[i];
					}
					delete[]_start;
				}
				_start = tmp;
				_finish = _start + old;
				_endofstorage = _start + n;
			}
		}

		void pop_back() {
			assert(size()>0);
			--_finish;
		}

		void push_back(const T& x) 
		{
			if (_finish==_endofstorage)
			{
				size_t newcapacity=capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}
			*_finish = x;
			++_finish;
		}

		iterator insert(iterator pos,const T& x) 
		{
			assert(pos >= _start);
			assert(pos <=_finish);
			if (_finish==_endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}
			//memmove(pos + 1, pos, sizeof(T) * (_finish - pos));
			iterator end = _finish - 1;
			while (end >= pos) {
				*(end + 1) = *end;
				--end;
			}
			*pos = x;
			++_finish;
			return pos;
		}

		void resize(size_t n,T val=T()) {
			/*if (n > capacity()) {
				reserve(n);
				while (_finish!=_endofstorage)
				{
					push_back(val);
					_finish++;
				}
			}
			if (n<=capacity() && n>=size())
			{
				_endofstorage -= (capacity() - n);
			}
			if (n<size())
			{
				_endofstorage -= (capacity() - n);
				_finish = _endofstorage;
			}*/
			if (n>size())
			{
				reserve(n);
				while (_finish<_start+n)
				{
					*_finish = val;
					_finish++;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}

		iterator erase(iterator pos) 
		{
			assert(pos >= _start);
			assert(pos < _finish);
			iterator it= pos + 1;
			while (it < _finish) {
				*(it - 1) = *it;
				*it++;
			}
			_finish--;
			return pos;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endofstorage - _start;
		}

		T& operator[](size_t n) {
			assert(n < size());
			return _start[n];
			//return *(_start + n);
		}

		const T& operator[](size_t n) const {
			assert(n < size());
			return _start[n];
			//return *(_start + n);
		}

		void print_vector(const vector<int>& v)  {
			cout << endl;
			for (auto ch : v)
			{
				cout << ch << " ";
			}
			cout << endl;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};

	void test_vector1() {
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);
		v.push_back(7);
		v.push_back(8);
		vector<int>::iterator it = v.begin();

		while (it != v.end())
		{
			if (*it%2==0) {
				it = v.erase(it);
			}
			else
			{
				it++;
			}
		}

		for (auto ch : v)
		{
			cout << ch << " ";
		}
		cout << endl;
		cout << endl;

		/*for(auto ch :v)
		{
			cout << ch << " ";
		}
		cout << endl;
		
		for (int i = 0; i < v.size(); i++)
		{
			cout << v[i] << " ";
		}
		cout << endl;

		v.print_vector(v);

		v.insert(v.begin(), 100);
		v.insert(v.begin(), 100);
		v.print_vector(v);

		vector<int> v2;
		v2 = v;
		cout <<"edwf"<<endl;
		v2.print_vector(v2);*/
		/*for (auto ch : v)
		{
			cout << ch << " ";
		}
		cout << endl;
		cout << endl;

		v.erase(v.begin()+3);

		for (auto ch : v)
		{
			cout << ch << " ";
		}
		cout << endl;
		cout << endl;*/

		/*v.resize(12,0);

		for (auto ch : v)
		{
			cout << ch << " ";
		}
		cout << endl;
		cout << endl;

		v.resize(2);

		for (auto ch : v)
		{
			cout << ch << " ";
		}
		cout << endl;
		cout << endl;*/
	}
}

//class Solution {
//public:
//	int removeDuplicates(vector<int>& nums) {
//		int n = nums.size();
//		if (n == 0)
//			return 0;
//		int fast = 1;
//		int slow = 1;
//		while (fast < n) {
//			if (nums[slow] != nums[fast]) {
//				nums[slow] = nums[fast];
//				++slow;
//			}
//			++fast;
//		}
//		return slow;
//	}
//};